/**
 *	QuebraLink - Quebrador de links que tem protetor
 *  Copyright (C) 2011  Ranis <ranishot@gmail.com>
 * 
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *  Classe de Teste de Novos Algoritmos
 *
 *	Esta Classe define todos os testes com novos algoritmos a fim de que se
 *      corrija qualquer tipo de bug sem ter que se manipular o algoritmo principal
 *      até que se tenha um código estável
 *
 *	@package QuebraLink
 *	@name QuebraLink.java
 *	@author	Ranishot <ranishot@gmail.com>
 *	@copyright (C)2010 - 2011 Ranishot.
 *	@contributors Rodrigo Borges <rodrigo@rbmsoft.com.br>
 *	@license GNU GPL v3
 *	@version 3.5
 *	@since 14/06/2011
 */
package quebralink;

import java.util.logging.Level;
import java.util.logging.Logger;
import sun.misc.BASE64Decoder;

public class QuebraLinkDebugger {
    // classe criada com o objetivo de depurar partes do codigo especificas
    // para facilitar e agilizar o processo de depuração

    public static void main(String[] args) {
        QuebraLinkDebugger a = new QuebraLinkDebugger();
        a.ql();
        
    }

    public void ql() {
        int cont = 0;
        char letra = '\0';
        String ltmp = "";
        String newlink = "";
        String linkOrig = "http://reidodownload.net/redirect/download2/index.php?id=355807894013570158588780187170019609401289016809476012831194701799499580984684868801684858911701801001576119019887288889388018838121777877472870178121111968987947015898755810141109070978076017709001279902198801787635786898660112870107221793721180110128474848872119477501687014827948458111686878";

        for (int i = linkOrig.length() - 4; i >= 0; i--) {
            letra = linkOrig.charAt(i);
            if (letra == '=' || letra == '?') {
                for (int j = i + 1; j < linkOrig.length(); j++) {
                    if (linkOrig.charAt(j) == '.') {
                        break;
                    }
                    newlink += linkOrig.charAt(j);


                }
                break;
            }
        }
        
        ltmp = newlink;
        String tmp = "";
        String tmp2 = "";
        do {

//hexadecimal decode inicio
            try {
                tmp = HexToChar(ltmp);
                tmp2 = HexToChar(inverteFrase(ltmp));
                if (!tmp.isEmpty() && tmp.length() > 5 && !tmp.contains("") && !tmp.contains("§") && !tmp.contains("�") && !tmp.contains("")) {
                  //  System.out.println("1");
                    ltmp = HexToChar(ltmp);
                  //  System.out.println(ltmp);
                } else if (!inverteFrase(tmp2).isEmpty() && inverteFrase(tmp2).length() > 5 && !inverteFrase(tmp2).contains("") && !inverteFrase(tmp2).contains("§") && !inverteFrase(tmp2).contains("�")) {
                  // System.out.println("2");
                    ltmp = HexToChar(inverteFrase(ltmp));
                  //  System.out.println(ltmp);
                }
            } catch (NumberFormatException e) {
            }//hexadecimal decode inicio
            
            
            
            //base64 decode inicio
            tmp = decode64(ltmp);
            tmp2 = decode64(inverteFrase(ltmp));
            if (!tmp.contains("�") && !tmp.contains("ޚ")) {
                //System.out.println("3");
                ltmp = decode64(ltmp);
                //System.out.println(ltmp);
            } else if (!tmp2.contains("�") && !tmp2.contains("ޚ")) {
                //System.out.println("4");
                ltmp = decode64(inverteFrase(ltmp));
            }//base64 decode fim
            
            
            
            //ASCII decode inicio
            try{
            tmp = decodeAscii(ltmp);
            tmp2 = decodeAscii(inverteFrase(ltmp));
            if(!tmp.contains("") && !tmp.contains("�") && !tmp.contains("§") && !tmp.contains("½") && !tmp.contains("*") && !tmp.contains("\"") && !tmp.contains("^")){
                //System.out.println("5");    
                ltmp = decodeAscii(ltmp);
                //System.out.println(ltmp);
            } else if (!tmp2.contains("") && !tmp2.contains("�") && !tmp2.contains("§") && !tmp2.contains("½") && !tmp2.contains("*") && !tmp2.contains("\"") && !tmp2.contains("^")){
                ltmp = decodeAscii(inverteFrase(ltmp));
                //System.out.println("6");
            }

            }catch(NumberFormatException e){
            
            }//ASCII decode fim
            

            cont++;
            if(ltmp.contains("http")){
                newlink = ltmp;
          //      System.out.println("cont: "+cont);
                break;
            } else if(ltmp.contains("ptth")){
                newlink = inverteFrase(ltmp);
          //      System.out.println("cont: "+cont);
                break;
            }
            
        } while (!isValid(newlink) && cont <= 20);
        System.out.println(newlink);
    }

    private boolean precisaRepassar(String link) {
        if (link.contains("lockurl")) {
            return true;
        }
        if (link.length() > 8) {
            if (link.substring(8).contains("http") || link.substring(8).contains("ptth")) {
                return true;
            } else {
                return false;
            }

        } else {
            return false;
        }
    }

    private String[] getLibrary() {

        String[] library = {"megaupload.com", "fileserve", "filesonic", "hotfile.com", "uploadstation", "easy-share", "rapidshare", "mediafire", "4shared", "bitroad", "depositfiles", "filebase", "freakshare", "multiupload", "zshare", "2shared", "migre", "ul.to", "uploaded.to", "oron.com", "bitshare", "uploading", "yess.me", "megavideo", "netload", "ziddu", "filebase.to", "wupload.com", "bit.ly", "goo.gl", "up-file.com", "letitbit.net"};
        return library;
    }

    private boolean isValid(String link) {
        for (String tmp : getLibrary()) {
            if (link.toLowerCase().contains(tmp)) {
                return true;
            }
        }
        return false;
    }

    private static String HexToChar(String hex) throws NumberFormatException {
        char value[] = new char[hex.length() / 2];
        int hexLength = value.length;
        for (int i = 0; i < hexLength; i++) {
            value[i] = (char) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
        }
        return new String(value);
    }

    private String decodeAscii(String ascii) throws NumberFormatException {
//algoritmo de decode sem escape ou caractere marcador criado por mim

        char num = 0;
        int num2 = 0;
        String recebe = "";
        String convertida = "";

        for (int j = 0; j < ascii.length(); j++) {
            num = ascii.charAt(j);

            recebe = recebe + num;
            num2 = Integer.parseInt(recebe);


            //converte uma sequência de caracteres acii para String
            if ((num2 >= 32) && (num2 <= 319)) {
                num = (char) num2;
                convertida += num;
                num2 = 0;
                recebe = "";
            }


        }
        return convertida;
    }

    private static String decode64(String TextoIni) {
        String textFinal = "";
        try {
            String TextoDecript = new String(new BASE64Decoder().decodeBuffer(TextoIni));
            textFinal = TextoDecript;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return textFinal;
    }

    private static String inverteFrase(String frase) {
        String newfrase = "";


        for (int i = frase.length() - 1; i >= 0; i--) {
            newfrase += frase.charAt(i);

        }
        return newfrase;
    }
    private void sleep(int miliseconds){
        try {
            Thread.sleep(miliseconds);
        } catch (InterruptedException ex) {
            Logger.getLogger(QuebraLinkDebugger.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
}
